/**
 * 公共请求库
 */
import { fetchCurrentEnv } from './config.js'
import { loading, clearLoading } from './loading'
export default function request({
  url = '/',
  data,
  header = {},
  method = 'GET',
  responseType = 'text'
}, isLoading = true) {
  // 默认显示loading图标
  if (isLoading) {
    loading()
  }
  return new Promise((resolve, reject) => {
    const env = getApp().globalData.env
    wx.request({
      // 如果接口调用的是第三方接口，则不补全全局域名
      url: (url.indexOf("https") > -1 || url.indexOf("http") > -1) ? url : fetchCurrentEnv(env) + url, // 设置基础url
      data,
      header,
      method,
      responseType,
      success: (data) => {
        // 返回服务器返回的数据
        clearLoading()
        resolve(data.data)
      },
      fail: (e) => {
        clearLoading()
        reject(e.data)
      }
    })
  })
}